home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / ipc / mainmsgqser1.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  73 lines

  1. #include    <stdio.h>
  2. #include    "mesg.h"
  3. #include    "msgq.h"
  4.  
  5. Mesg    mesg;
  6.  
  7. main()
  8. {
  9.     int    id;
  10.  
  11.     /*
  12.      * Create the message queue, if required.
  13.      */
  14.  
  15.     if ( (id = msgget(MKEY1, PERMS | IPC_CREAT)) < 0)
  16.         err_sys("server: can't get message queue 1");
  17.  
  18.     server(id);
  19.  
  20.     exit(0);
  21. }
  22.  
  23. server(id)
  24. int    id;
  25. {
  26.     int    n, filefd;
  27.     char    errmesg[256], *sys_err_str();
  28.  
  29.     /*
  30.      * Read the filename message from the IPC descriptor.
  31.      */
  32.  
  33.     mesg.mesg_type = 1L;        /* receive messages of this type */
  34.     if ( (n = mesg_recv(id, &mesg)) <= 0)
  35.         err_sys("server: filename read error");
  36.     mesg.mesg_data[n] = '\0';    /* null terminate filename */
  37.  
  38.     mesg.mesg_type = 2L;        /* send messages of this type */
  39.     if ( (filefd = open(mesg.mesg_data, 0)) < 0) {
  40.         /*
  41.          * Error.  Format an error message and send it back
  42.          * to the client.
  43.          */
  44.  
  45.         sprintf(errmesg, ": can't open, %s\n", sys_err_str());
  46.         strcat(mesg.mesg_data, errmesg);
  47.         mesg.mesg_len = strlen(mesg.mesg_data);
  48.         mesg_send(id, &mesg);
  49.  
  50.     } else {
  51.         /*
  52.          * Read the data from the file and send a message to
  53.          * the IPC descriptor.
  54.          */
  55.  
  56.         while ( (n = read(filefd, mesg.mesg_data, MAXMESGDATA)) > 0) {
  57.             mesg.mesg_len = n;
  58.             mesg_send(id, &mesg);
  59.         }
  60.         close(filefd);
  61.  
  62.         if (n < 0)
  63.             err_sys("server: read error");
  64.     }
  65.  
  66.     /*
  67.      * Send a message with a length of 0 to signify the end.
  68.      */
  69.  
  70.     mesg.mesg_len = 0;
  71.     mesg_send(id, &mesg);
  72. }
  73.